#pragma once
#include <EGL/egl.h>  // EGL支持
#include <GLES3/gl3.h>
#include <android/log.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>  // ANativeWindow_fromSurface

#include <chrono>
#include <cmath>
#include <thread>

#include "include/checkError.h"
#include "include/shader.h"
#include "vector"
extern "C" {
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/avutil.h>
#include <libavutil/time.h>
}
namespace codec {
class VideoRender {
 public:
  struct EGLEnvironment {
    EGLDisplay display;
    EGLSurface surface;
    EGLContext context;
  };
  void controlFrameRate(
      std::chrono::high_resolution_clock::time_point start_time, int64_t pts,
      int64_t& previous_pts, AVRational time_base);
  bool init(ANativeWindow* window, const std::string& vertexCode,
            const std::string& fragmentCode);
  void paprevao();
  void prepareShader(const std::string& vertexCode,
                     const std::string& fragmentCode);
  void renderFrame(AVFrame* frame);
  void updateViewPort(int width, int height);
  void release();

 private:
  ANativeWindow* native_window_;
  EGLEnvironment egl_env_;
  std::chrono::time_point<std::chrono::high_resolution_clock> last_frame_time_;
  GLuint vao_;
  GLuint textures[3];
  Shader* shader = nullptr;
};
}  // namespace codec